<?php

class Acakm extends MX_Controller {

	var $partials = array();
	var $sitePermissions = array();
	var $includes_path = '/includes';			// path to includes for header and footer
	var $num = 10;

	function __construct()
	{
		parent::__construct();

		// get siteID, if available
		if (defined('SITEID'))
		{
			$this->siteID = SITEID;
		}		

		// get site permissions and redirect if it don't have access to this module
		//if (!$this->permission->sitePermissions)
		//{
		//	show_error('You do not have permission to view this page');
		//}
		//if (!in_array($this->uri->segment(1), $this->permission->sitePermissions))
	//	{
	//		show_error('You do not have permission to view this page');
	//	}

		// load models and modules
		$this->load->library('tags');
		$this->load->model('acakm_model', 'acakm');
		$this->load->module('pages');
		// load language demo
		$this->load->language('english');
	}

	//acakm
	function index()
	{
		// load partials - categories
		if ($cats = $this->acakm->get_cats())
		{
			foreach($cats as $cat)
			{
				$this->partials['blog:categories'][] = array(
					'category:link' => site_url('/acakm/'.$cat['feeds_cat_id']),
					'category:title' => $cat['feeds_cat_name'],
					'category:count' => $cat['numPosts']
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		$posts = $this->acakm->get_posts($this->num);
		$output['feeds:posts'] = $this->_populate_posts($posts);

		// set title
		$output['page:title'] = 'acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');


		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// display with cms layer
		$this->view('acakm', $output, TRUE);
	}
	
	function teching_media($i_section='')
	{
		// load partials - section
		if ($cats = $this->acakm->get_tm_section())
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:section'][] = array(
					'category:link' => site_url('/acakm/teching_media/'.$cat['section_id']),
					'category:section_id' => $cat['section_id']
				);
			}
		}
		// load partials - categories
		if ($cats = $this->acakm->get_teching_media_cats())
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:grade'][] = array(
					'grade:link' => site_url('/acakm/teching_media/'.$cat['section_id'].'/'.$cat['grade_id']),
					'grade:section_id' => $cat['section_id'],
					'grade:grade_id' => $cat['grade_id'],
					'grade:label_thai' => $cat['label_thai'],
					'grade:label_english' => $cat['label_english'],
					'grade:count' => $cat['cnt']
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		$posts = $this->acakm->get_teching_media_posts($this->num,$i_section);
		$output['feeds:posts'] = $this->_populate_teching_media_posts($posts);

		// set title
		$output['page:title'] = 'acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// URL
		//$output['url'] = site_url('acakm/
		
		// display with cms layer
		$this->view('teching_media', $output, TRUE);
	}
	
	function exercise($s_section='',$i_grade='')
	{
		// load partials - section
		if ($cats = $this->acakm->get_tm_section())
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:section'][] = array(
					'category:link' => site_url('/acakm/exercise/'.$cat['section_id']),
					'category:section_id' => $cat['section_id']
				);
			}
		}
		// load partials - categories
		if ($cats = $this->acakm->get_exercise_cats($s_section))
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:grade'][] = array(
					'grade:link' => site_url('/acakm/exercise/'.$s_section.'/'.$cat['grade_id']),
					'grade:grade_id' => $cat['grade_id'],
					'grade:label_thai' => $cat['label_thai'],
					'grade:label_english' => $cat['label_english'],
					'grade:count' => $cat['cnt']
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		$posts = $this->acakm->get_exercise_posts($this->num,$s_section,$i_grade);
		$output['feeds:posts'] = $this->_populate_exercise_posts($posts);

		// set title
		$output['page:title'] = 'acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// URL
		//$output['url'] = site_url('acakm/
		
		// display with cms layer
		$this->view('exercise', $output, TRUE);
	}
	
	function do_ex($i_exercise='',$i_item='')
	{
		if($this->input->post('proc') == 'do_ex') {
			$this->acakm->add_score();
		}
		if($i_exercise=='' && $_POST['ex_id']!='') {
			$i_exercise = $_POST['ex_id'];
		}
		if($i_item=='') {
			$i_item = '1';
			$a_item = array('score' => '');
			$this->session->unset_userdata($a_item);
		}
		// load partials - categories
		if ($cats = $this->acakm->get_question_list($i_exercise))
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:question'][] = array(
					'question:name' => 'Item no '.$cat['exercise_no'],
					'question:click_event' => "document.getElementById('ex_id').value=".$cat['exercise_id']."; document.getElementById('frmEx').submit();"
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		$posts = $this->acakm->get_exercise_form($i_exercise,$i_item);
		$output['feeds:posts'] = $this->_populate_exercise_form($posts);

		// set title
		$output['page:item_list'] = 'Item list';
		$output['page:action'] = site_url('acakm/do_ex');
		$output['page:ex_id'] = $i_exercise;
		//$output['page:title'] = 'Item no '.$;
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_question'] = $this->lang->line('acakm_no_question');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// URL
		//$output['url'] = site_url('acakm/
		
		// display with cms layer
		$this->view('do_ex', $output, TRUE);
	}
	
	function summary_page($i_exercise='')
	{
		// load partials - categories
		if ($cats = $this->acakm->get_question_list($i_exercise))
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:question'][] = array(
					'question:name' => 'Item no '.$cat['exercise_no'],
					'question:click_event' => "document.getElementById('ex_id').value=".$cat['exercise_id']."; document.getElementById('frmEx').submit();"
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		//$posts = $this->acakm->get_exercise_form($i_exercise);
		//$output['feeds:posts'] = $this->_populate_exercise_form($posts);

		// set title
		$output['page:item_list'] = 'Item list';
		$output['page:title'] = 'Complete Exam:';
		$output['page:summary'] = 'Solution view';
		$output['page:score_summary'] = '1 Your score is ';
		$output['page:view_solution_link'] = site_url('acakm/view_solution/'.$i_exercise);
		$output['page:view_solution_title'] = '2 View solution';
		$output['page:ex_result'] = 'You Pass!';
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_question'] = $this->lang->line('acakm_no_question');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// URL
		//$output['url'] = site_url('acakm/
		
		// display with cms layer
		$this->view('summary_page', $output, TRUE);
	}
	
	function set_teching_plan($i_section='')
	{
		// load partials - section
		if ($cats = $this->acakm->get_tm_section())
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:section'][] = array(
					'category:link' => site_url('/acakm/teching_media/'.$cat['section_id']),
					'category:section_id' => $cat['section_id']
				);
			}
		}
		// load partials - categories
		if ($cats = $this->acakm->get_teching_media_cats())
		{
			foreach($cats as $cat)
			{
				$this->partials['acakm:grade'][] = array(
					'grade:link' => site_url('/acakm/teching_media/'.$cat['section_id'].'/'.$cat['grade_id']),
					'grade:section_id' => $cat['section_id'],
					'grade:grade_id' => $cat['grade_id'],
					'grade:label_thai' => $cat['label_thai'],
					'grade:label_english' => $cat['label_english'],
					'grade:count' => $cat['cnt']
				);
			}
		}
		// get partials
		$output = $this->partials;
		// get latest posts
		$posts = $this->acakm->get_teching_media_posts($this->num,$i_section);
		$output['feeds:posts'] = $this->_populate_teching_media_posts($posts);

		// set title
		$output['page:title'] = 'acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// URL
		//$output['url'] = site_url('acakm/
		
		// display with cms layer
		$this->view('set_teching_plan', $output, TRUE);
	}

	function more()
	{
		// get partials
		$output = $this->partials;
	
		// get latest posts
		$posts = $this->acakm->get_posts($this->num);
		$output['blog:posts'] = $this->_populate_posts($posts);

		// set title
		$output['page:title'] = 'acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/acakm/more/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/acakm', '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/acakm/more/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';

		// display with cms layer
		$this->view('acakm', $output, TRUE);
	}

	//acakm detail
	function read()
	{
		// get partials
		$output = $this->partials;	
				
		// get post based on uri
		$year = $this->uri->segment(2);
		$month = $this->uri->segment(3);
		$uri = $this->uri->segment(4);
		
		if ($post = $this->acakm->get_post($year, $month, $uri))
		{
			// add comment
			if (count($_POST))
			{
				// required
				$this->core->required = array(
					'fullName' => 'Full name',
					'comment' => 'Comment',
				);

				// check for spam
				preg_match_all('/http:\/\//i', $this->input->post('comment'), $urlMatches);
				preg_match_all('/viagra|levitra|cialis/i', $this->input->post('comment'), $spamMatches);			
				if (count($urlMatches[0]) > 2 || (count($urlMatches[0]) > 0 && count($spamMatches[0]) > 0))
				{
					$this->form_validation->set_error('Sorry but your comment looks like spam. Please remove links and try again.');
				}
				elseif (isset($_POST['captcha']) && !$this->_captcha_check())
				{
					$this->form_validation->set_error('Sorry you didn\'t pass the spam check. Please contact us to post a comment.');
				}
				else
				{			
					// set date
					$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
					$this->core->set['postID'] = $post['postID'];
	
					// awaiting moderation
					if ($this->session->userdata('session_admin'))
					{
						$this->core->set['active'] = 1;
					}
					else
					{
						$this->core->set['active'] = 0;
					}
					
					// update
					if ($this->core->update('acakm_comments'))
					{
						// get insertID
						$commentID = $this->db->insert_id();
	
						// get details on post poster		
						$user = $this->acakm->get_user($post['userID']);
	
						// construct URL
						$url = '/acakm/'.$year.'/'.$month.'/'.$uri.'/';					
						
						if ($user['notifications'] && !$this->session->userdata('session_admin'))
						{
							// set header and footer
							$emailHeader = str_replace('{name}', $user['firstName'].' '.$user['lastName'], $this->site->config['emailHeader']);
							$emailHeader = str_replace('{email}', $user['email'], $emailHeader);
							$emailFooter = str_replace('{name}', $user['firstName'].' '.$user['lastName'], $this->site->config['emailFooter']);
							$emailFooter = str_replace('{email}', $user['email'], $emailFooter);
							
							// send email
							$this->load->library('email');						
							$this->email->from($this->site->config['siteEmail'], $this->site->config['siteName']);
							$this->email->to($user['email']);			
							$this->email->subject('New acakm Comment on '.$this->site->config['siteName']);
							$this->email->message($emailHeader."\n\nSomeone has just commented on your acakm post titled \"".$post['postTitle']."\".\n\nYou can either approve or delete this comment by clicking on the following URL:\n\n".site_url('/admin/acakm/comments')."\n\nThey said:\n\"".$this->input->post('comment')."\"\n\n".$emailFooter);
							$this->email->send();
						}
	
						// output message
						$output['message'] = 'Thank you, your comment has been posted and is awaiting moderation.';
	
						// disable form
						$post['allowComments'] = 0;
					}
				}
			}
												
			// set page title
			$output['page:title'] = $post['postTitle'].(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
			
			// set meta description
			if ($post['excerpt'])
			{
				$output['page:description'] = $post['excerpt'];
			}

			// get author details
			$author = $this->acakm->lookup_user($post['userID']);

			// populate template
			$output['post:title'] = $post['postTitle'];
			$output['post:link'] = site_url('acakm/'.dateFmt($post['dateCreated'], 'Y/m').'/'.$post['uri']);
			$output['post:date'] = dateFmt($post['dateCreated'], ($this->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y');
			$output['post:day'] = dateFmt($post['dateCreated'], 'd');
			$output['post:month'] = dateFmt($post['dateCreated'], 'M');
			$output['post:year'] = dateFmt($post['dateCreated'], 'y');
			$output['post:body'] = $this->template->parse_body($post['body']);
			$output['post:excerpt'] = $this->template->parse_body($post['excerpt']);
			$output['post:comments-count'] = $post['numComments'];
			$output['post:author'] = (($author['displayName']) ? $author['displayName'] : $author['firstName'].' '.$author['lastName']);
			$output['post:author-id'] = $author['userID'];
			$output['post:author-email'] = $author['email'];
			$output['post:author-gravatar'] = 'http://www.gravatar.com/avatar.php?gravatar_id='.md5(trim($author['email'])).'&default='.urlencode(site_url('/static/uploads/avatars/noavatar.gif'));
			$output['post:author-bio'] = $author['bio'];
			$output['post:allow-comments'] = ($post['allowComments']) ? TRUE : FALSE;
			$output['form:name'] = set_value('fullName', $this->session->userdata('firstName').' '.$this->session->userdata('lastName'));
			$output['form:email'] = set_value('email', $this->session->userdata('email'));
			$output['form:website'] = $this->input->post('website');
			$output['form:comment'] = $this->input->post('comment');


			// set caption language
			$output['caption:acakm_add_a_comment'] = $this->lang->line('acakm_add_a_comment');
			$output['caption:acakm_back_to_acakm'] = $this->lang->line('acakm_back_to_acakm');
			$output['caption:acakm_posted'] = $this->lang->line('acakm_posted');
			$output['caption:acakm_posted_by'] = $this->lang->line('acakm_posted_by');
			$output['caption:acakm_tags'] = $this->lang->line('acakm_tags');
			$output['caption:acakm_comments'] = $this->lang->line('acakm_comments');
			$output['caption:acakm_by'] = $this->lang->line('acakm_by');
			$output['caption:acakm_on'] = $this->lang->line('acakm_on');
			$output['caption:acakm_add_a_comment'] = $this->lang->line('acakm_add_a_comment');
			$output['caption:acakm_your_name'] = $this->lang->line('acakm_your_name');
			$output['caption:acakm_your_email'] = $this->lang->line('acakm_your_email');
			$output['caption:acakm_your_website'] = $this->lang->line('acakm_your_website');
			$output['caption:acakm_comment'] = $this->lang->line('acakm_comment');
			$output['caption:acakm_post_comment'] = $this->lang->line('acakm_post_comment');
			$output['caption:acakm_back_to_top'] = $this->lang->line('acakm_back_to_top');

			// get cats
			if ($cats = $this->blog->get_cats_for_post($post['postID']))
			{
				$i = 0;
				foreach ($cats as $cat)
				{
					$output['post:categories'][$i]['category:link'] = site_url('blog/'.url_title(strtolower(trim($cat))));
					$output['post:categories'][$i]['category'] = $cat;
					
					$i++;
				}
			}

			// get tags
			if ($post['tags'])
			{
				$tags = explode(',', $post['tags']);
				
				$i = 0;
				foreach ($tags as $tag)
				{
					$output['post:tags'][$i]['tag:link'] = site_url('blog/tag/'.$this->tags->make_safe_tag($tag));
					$output['post:tags'][$i]['tag'] = $tag;
					
					$i++;
				}
			}

			// get comments
			if ($comments = $this->blog->get_comments($post['postID']))
			{
				$i = 0;
				foreach ($comments as $comment)
				{
					$output['post:comments'][$i]['comment:class'] = ($i % 2) ? ' alt ' : '';
					$output['post:comments'][$i]['comment:id'] = $comment['commentID'];
					$output['post:comments'][$i]['comment:gravatar'] = 'http://www.gravatar.com/avatar.php?gravatar_id='.md5(trim($comment['email'])).'&default='.urlencode(site_url('/static/uploads/avatars/noavatar.gif'));
					$output['post:comments'][$i]['comment:author'] = (!empty($comment['website'])) ? anchor(prep_url($comment['website']), $comment['fullName']) : $comment['fullName'];
					$output['post:comments'][$i]['comment:date'] = dateFmt($comment['dateCreated'], ($this->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y');
					$output['post:comments'][$i]['comment:body'] = nl2br(auto_link(strip_tags($comment['comment'])));
					
					$i++;
				}
			}
		
			// load errors
			$output['errors'] = (validation_errors()) ? validation_errors() : FALSE;

			// add view
			$this->blog->add_view($post['postID']);

			// output post ID for CMS
			$output['postID'] = $post['postID'];
			
			// display with cms layer
			$this->view('acakm_single', $output, TRUE);
		}
		else
		{
			show_404();
		}
	}

	function tag($tag = '')
	{
		// get partials
		$output = $this->partials;	

		// get posts
		$posts = $this->blog->get_posts_by_tag($tag);
		$output['blog:posts'] = $this->_populate_posts($posts);
		
		// output tag tags
		$output['tag:title'] = ucwords(str_replace('-', ' ', $tag));
		$output['tag:link'] = '/blog/tag/'.$tag;

		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/blog/tag/'.$tag.'/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/blog/tag/'.$tag, '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/blog/tag/'.$tag.'/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';	
		
		// set title
		$output['page:title'] = ucwords(str_replace('-', ' ', $tag)).(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		$output['page:heading'] = '<small>Tag:</small> '.ucfirst(str_replace('-', ' ', $tag));
		
		// display with cms layer
		$this->view('blog', $output, TRUE);
	}

	function category($cat = '')
	{
		// get partials
		$output = $this->partials;

		// get posts
		$posts = $this->blog->get_posts_by_category($cat);
		$output['blog:posts'] = $this->_populate_posts($posts);
		
		// output category tags
		$output['category:title'] = ucwords(str_replace('-', ' ', $cat));
		$output['category:link'] = '/blog/'.$cat;
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/blog/'.$cat.'/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/blog/'.$cat, '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/blog/'.$cat.'/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';
		
		// set title
		$output['page:title'] = ucwords(str_replace('-', ' ', $cat)).(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		$output['page:heading'] = '<small>Category:</small> '.ucwords(str_replace('-', ' ', $cat));
		
		// display with cms layer
		$this->view('blog', $output, TRUE);
	}

	function month()
	{
		// get partials
		$output = $this->partials;	
				
		// get post based on uri
		$year = $this->uri->segment(2);
		$month = $this->uri->segment(3);

		// get posts
		$posts = $this->blog->get_posts_by_date($year, $month);
		$output['blog:posts'] = $this->_populate_posts($posts);

		// set title
		$output['page:title'] = 'Posts For '.date('F Y', mktime(0,0,0,$month,1,$year)).(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		$output['page:heading'] = '<small>Archive:</small> '.date('F Y', mktime(0,0,0,$month,1,$year));

		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/blog/'.$year.'/'.$month.'/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/blog/'.$year.'/'.$month, '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/blog/'.$year.'/'.$month.'/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';	
				
		// display with cms layer
		$this->view('blog', $output, TRUE);	
	}

	function year()
	{
		// get partials
		$output = $this->partials;

		// get post based on uri
		$year = $this->uri->segment(2);	

		// get tags
		$posts = $this->blog->get_posts_by_date($year);
		$output['blog:posts'] = $this->_populate_posts($posts);

		// set title
		$output['page:title'] = 'Posts For '.date('Y', mktime(0,0,0,1,1,$year)).(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		$output['page:heading'] = '<small>Archive:</small> '.date('Y', mktime(0,0,0,1,1,$year));
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');

		// set pagination and breadcrumb
		$output['blog:newer'] = ($this->pagination->offset) ? 
			(($this->pagination->offset - $this->num > 0) ? 
				anchor('/blog/'.$year.'/page/'.($this->pagination->offset - $this->num), '&laquo; Newer') :
				anchor('/blog/'.$year, '&laquo; Newer'))
		: '';
		$output['blog:older'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/blog/'.$year.'/page/'.($this->pagination->offset + $this->num), 'Older &raquo;') : '';	
		
		// display with cms layer
		$this->view('blog', $output, TRUE);	
	}

	function search($query = '')
	{
		// get partials
		$output = $this->partials;	

		// set tags
		$query = ($query) ? $query : $this->input->post('query');

		// get result from tags
		$objectIDs = $this->tags->search('acakm_posts', $query);

		$posts = $this->acakm->search_posts($query, $objectIDs);
		$output['blog:posts'] = $this->_populate_posts($posts);
		$output['query'] = $query;

		// set title
		$output['page:title'] = 'Search the acakm'.(($this->site->config['siteName']) ? ' - '.$this->site->config['siteName'] : '');
		$output['page:heading'] = 'Search for "'.$output['query'].'"';	
		
		// set caption language
		$output['caption:acakm_categories'] = $this->lang->line('acakm_categories');
		$output['caption:acakm_no_categories_set_yet'] = $this->lang->line('acakm_no_categories_set_yet');
		$output['caption:acakm_there_are_no_posts_yet'] = $this->lang->line('acakm_there_are_no_posts_yet');
		$output['caption:acakm_search_posts'] = $this->lang->line('acakm_search_posts');
		$output['caption:acakm_recent_posts'] = $this->lang->line('acakm_recent_posts');
		$output['caption:acakm_no_posts_yet'] = $this->lang->line('acakm_no_posts_yet');
		$output['caption:acakm_archive'] = $this->lang->line('acakm_archive');
		$output['caption:acakm_nothing_to_archive_yet'] = $this->lang->line('acakm_nothing_to_archive_yet');
		$output['caption:acakm_subscribe_to_feed'] = $this->lang->line('acakm_subscribe_to_feed');
		$output['caption:acakm_acakm_rss_feed'] = $this->lang->line('acakm_acakm_rss_feed');


		// set pagination and breadcrumb
		$output['blog:older'] = ($this->pagination->offset) ? anchor('/blog/more/page/'.($this->pagination->offset - $this->num), 'Older &raquo;') : '';
		$output['blog:newer'] = ($this->pagination->total_rows > ($this->pagination->offset + $this->num)) ? anchor('/blog/more/page/'.($this->pagination->offset + $this->num), '&laquo; Newer') : '';
		
		// display with cms layer
		$this->view('acakm_search', $output, TRUE);		
	}
	
	function feed($cat = '')
	{
		// rss feed
		$this->load->helper('xml');
		
		$data['encoding'] = 'utf-8';
		$data['page_language'] = 'en';
		$data['creator_email'] = $this->site->config['siteEmail'];
		
		// get posts by category
		if ($cat)
		{
			$category = ucwords(str_replace('-', ' ', $cat));
			
			$data['feed_name'] = $this->site->config['siteName'].' - '.$category;
			$data['feed_url'] = site_url('/acakm/'.$cat);
			$data['page_description'] = 'acakm Category RSS Feed for '.$this->site->config['siteName'].' - '.$category.'.';			
			
			$data['posts'] = $this->blog->get_posts_by_category($cat);
		}
		
		// get latest posts
		else
		{
			$data['feed_name'] = $this->site->config['siteName'];
			$data['feed_url'] = site_url('/blog');
			$data['page_description'] = 'Blog RSS Feed for '.$this->site->config['siteName'].'.';
			
			$data['posts'] = $this->blog->get_posts(10);
		}
		
		$this->output->set_header('Content-Type: application/rss+xml');
		$this->load->view('acakm/rss', $data);
	}

	function ac_search()
	{
		$tags = strtolower($_POST["q"]);
        if (!$tags)
        {
        	return FALSE;
        }

		if ($objectIDs = $this->tags->search('acakm_posts', $tags))
		{		
			// form dropdown and myql get countries
			if ($searches = $this->acakm->search_posts($objectIDs))
			{
				// go foreach
				foreach($searches as $search)
				{
					$items[$search['tags']] = array('id' => dateFmt($search['dateCreated'], 'Y/m').'/'.$search['uri'], 'name' => $search['postTitle']);
				}
				foreach ($items as $key=>$value)
				{
					$id = $value['id'];
					$name = $value['name'];
					/* If you want to force the results to the query
					if (strpos(strtolower($key), $tags) !== false)
					{
						echo "$key|$id|$name\n";
					}*/
					$this->output->set_output("$key|$id|$name\n");
				}
			}
		}
	}

	function _captcha_check()
	{
		// if captcha is posted, check its not a bot (requires js)
		if ($this->input->post('captcha') == 'notabot')
		{
			return TRUE;
		}
		elseif ($this->input->post('captcha') != 'notabot')
		{
			$this->form_validation->set_message('captcha_check', 'You didn\'t pass the spam check, please contact us to post a comment.');
			return FALSE;
		}
	}

	function _populate_posts($posts = '')
    {
    	if ($posts && is_array($posts))
    	{
			$x = 0;
			foreach($posts as $post)
			{
				// get author details
				//$author = $this->acakm->lookup_user($post['userID']);				
				
				// populate template array
				$data[$x] = array(
					'post:link' => site_url('acakm/'.dateFmt($post['updated_date'], 'Y/m').'/'.$post['feeds_url']),
					'post:title' => $post['feeds_title'],
					'post:date' => dateFmt($post['updated_date'], ($this->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
					'post:post_date' => dateFmt($post['updated_date'], 'd/m/Y'),
					'post:day' => dateFmt($post['updated_date'], 'd'),
					'post:month' => dateFmt($post['updated_date'], 'M'),
					'post:year' => dateFmt($post['updated_date'], 'y'),										
					'post:body' => $this->template->parse_body($post['feeds_description'], TRUE, site_url('blog/'.dateFmt($post['updated_date'], 'Y/m').'/'.$post['feeds_url'])),
					'post:excerpt' => $this->template->parse_body($post['feeds_description'], TRUE, site_url('blog/'.dateFmt($post['updated_date'], 'Y/m').'/'.$post['feeds_url'])),
					'post:language_name' => $post['language_name']//,
					//'post:author' => (($author['displayName']) ? $author['displayName'] : $author['firstName'].' '.$author['lastName']),
					//'post:author-id' => $author['userID'],
					//'post:author-email' => $author['email'],
					//'post:author-gravatar' => 'http://www.gravatar.com/avatar.php?gravatar_id='.md5(trim($author['email'])).'&default='.urlencode(site_url('/static/uploads/avatars/noavatar.gif')),
					//'post:author-bio' => $author['bio'],
					//'post:comments-count' => $post['numComments']
				);
	
				// get cats
				if ($cats = $this->acakm->get_cats_for_post($post['feeds_id']))
				{
					$i = 0;
					foreach ($cats as $cat)
					{
						$data[$x]['post:categories'][$i]['category:link'] = site_url('blog/'.url_title(strtolower(trim($cat))));
						$data[$x]['post:categories'][$i]['category'] = $cat;
						
						$i++;
					}
				}
				
				// get tags
				/*if ($post['tags'])
				{
					$tags = explode(',', $post['tags']);

					$i = 0;
					foreach ($tags as $tag)
					{
						$data[$x]['post:tags'][$i]['tag:link'] = site_url('blog/tag/'.$this->tags->make_safe_tag($tag));
						$data[$x]['post:tags'][$i]['tag'] = $tag;
						
						$i++;
					}
				}*/
	
				$x++;
			}

			return $data;
		}
		else
		{
			return FALSE;
		}
    }
	
	function _populate_teching_media_posts($posts = '')
    {
    	if ($posts && is_array($posts))
    	{
			$x = 0;
			foreach($posts as $post)
			{
				// get author details
				//$author = $this->acakm->lookup_user($post['userID']);				
				
				// populate template array
				$data[$x] = array(
					'post:link' => site_url('acakm/'.$post['plan_id']),
					'post:title' => $post['teching_media_name_th'],
					'post:date' => dateFmt($post['publish_date'], ($this->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
					'post:post_date' => dateFmt($post['updated_date'], 'd/m/Y'),
					'post:day' => dateFmt($post['updated_date'], 'd'),
					'post:month' => dateFmt($post['updated_date'], 'M'),
					'post:year' => dateFmt($post['updated_date'], 'y'),										
					'post:body' => $this->template->parse_body($post['content'], TRUE, site_url('acakm/'.dateFmt($post['updated_date'], 'Y/m').'/'.$post['plan_id'])),
					'post:language_name' => $post['language_name']
				);
				$x++;
			}

			return $data;
		}
		else
		{
			return FALSE;
		}
    }
	function _populate_exercise_posts($posts = '')
    {
    	if ($posts && is_array($posts))
    	{
			$x = 0;
			foreach($posts as $post)
			{
				// get author details
				//$author = $this->acakm->lookup_user($post['userID']);				
				
				// populate template array
				$data[$x] = array(
					'post:link' => site_url('acakm/do_ex/'.$post['exercise_id']),
					'post:title' => 'Exercise '.$post['exercise_no'].' for class '.$post['grade_id'],
					'post:date' => dateFmt($post['publish_date'], ($this->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
					'post:post_date' => dateFmt($post['updated_date'], 'd/m/Y'),
					'post:day' => dateFmt($post['updated_date'], 'd'),
					'post:month' => dateFmt($post['updated_date'], 'M'),
					'post:year' => dateFmt($post['updated_date'], 'y'),										
					'post:body' => $this->template->parse_body($post['exercise_name_tha'], TRUE, site_url('acakm/'.dateFmt($post['updated_date'], 'Y/m').'/'.$post['exercise_id']))
				);
				$x++;
			}

			return $data;
		}
		else
		{
			return FALSE;
		}
    }
	function _populate_exercise_form($posts = '')
    {
    	if ($posts && is_array($posts))
    	{
			$x = 0;
			foreach($posts as $post)
			{
				// get author details
				//$author = $this->acakm->lookup_user($post['userID']);				
				
				// populate template array
				$data[$x] = array(
					'post:ex_id' => $post['exercise_id'],
					'post:item_id' => $post['item_id'],
					'post:item_title' => 'Item No. '.$post['item_id'],
					'post:exercise_no' => 'Exercise '.$post['exercise_no'].' for Class '.$post['grade_id'],
					'post:exercise_name_tha' => $post['exercise_name_tha'],
					'post:item_question_tha' => $post['item_question_tha'],
					'post:item_choice1_text_tha' => '1. '.$post['item_choice1_text_tha'],
					'post:item_choice2_text_tha' => '2. '.$post['item_choice2_text_tha'],
					'post:item_choice3_text_tha' => '3. '.$post['item_choice3_text_tha'],
					'post:item_choice4_text_tha' => '4. '.$post['item_choice4_text_tha'],
					'post:item_question_eng' => $post['item_question_eng'],
					'post:item_choice1_text_eng' => $post['item_choice1_text_eng'],
					'post:item_choice2_text_eng' => $post['item_choice2_text_eng'],
					'post:item_choice3_text_eng' => $post['item_choice3_text_eng'],
					'post:item_choice4_text_eng' => $post['item_choice4_text_eng'],
					'post:item_answer' => $post['item_answer'],
					'post:click_event1' => "document.getElementById('selected_choice').value=1; document.getElementById('frmEx').submit();",
					'post:click_event2' => "document.getElementById('selected_choice').value=2; document.getElementById('frmEx').submit();",
					'post:click_event3' => "document.getElementById('selected_choice').value=3; document.getElementById('frmEx').submit();",
					'post:click_event4' => "document.getElementById('selected_choice').value=4; document.getElementById('frmEx').submit();"
				);
				$x++;
			}

			return $data;
		}
		else
		{
			return FALSE;
		}
    }

	function view($view_name,$output,$flag) 
	{	
		//header
		$this->load->view($this->includes_path.'/header');
		//echo "view=".$view_name;
		//Body
		$this->view_template($view_name,$output,$flag);
		
		//Footer
 		$this->load->view($this->includes_path.'/footer');

	}

	function view_template($page, $sendthru = '', $module = FALSE, $return = FALSE)
	{	
		// set default parse file
		$parseFile = 'default';
		
		// handle web form
		if (count($_POST) && !$module)
		{
			if ($message = $this->core->web_form())
			{
				$sendthru['message'] = $message;
				$this->template->template['message'] = $sendthru['message'];
			}
			else
			{
				$sendthru['errors'] = validation_errors();
				$this->template->template['errors'] = $sendthru['errors'];
			}
		}

		// see if the cms is to generate a page from a module or a function of the site
		if ($module)
		{
			// set template tag
			$this->template->template['page:template'] = $page;
			
			// get module name
			$module = (is_string($module)) ? $module : $this->uri->segment(1);

			// get module template
			if ($file = @file_get_contents(APPPATH.'modules/'.$module.'/views/templates/'.$page.'.php'))
			{	
				// make a template out of the file
				$module = $this->template->generate_template(FALSE, $file);

				// merge the sendthru data with page data		
				$template = (is_array($sendthru)) ? array_merge($module, $sendthru) : $module;

				// set a null title
				$template['page:title'] = (!isset($sendthru['page:title'])) ? $this->site->config['siteName'] : $sendthru['page:title'];


				// output data
				if ($return === FALSE)
				{
					$this->parser->parse($parseFile, $template);
				}
				else
				{
					return $this->parser->parse($parseFile, $template, TRUE);
				}
			}
			else
			{
				show_error('Templating error!');
			}
		}
		else
		{
			show_404();
		}		
	}

}